package com.itgacl.magic4j.libcommon.component.delayQueue.controller;

import com.itgacl.magic4j.libcommon.bean.R;
import com.itgacl.magic4j.libcommon.component.delayQueue.service.DelayQueueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 延迟消息API控制器
 *
 */
@Slf4j
@Api(tags = "延迟消息API")
@RestController
@RequestMapping("/message/push")
public class DelayQueueController {

    @Autowired
    private DelayQueueService delayQueueService;


    /**
     * 正常消息，排队出去
     *
     * @param message 消息
     * @return
     */
    @ApiOperation(value = "发送正常消息",notes = "正常消息，排队出去")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "msgId",value = "消息ID",required = true),
            @ApiImplicitParam(name = "message",value = "消息内容",required = true)
    })
    @PostMapping("/normal")
    public R<Void> normal(@RequestParam("msgId") String msgId, @RequestParam("message") String message) {
        delayQueueService.commit(msgId, message);
        return R.ok();
    }

    /**
     * 进入延迟队列，等待时间正常发送出去
     *
     * @param message   消息
     * @param timestamp 时间戳
     * @return
     */
    @ApiOperation(value = "发送延迟消息",notes = "进入延迟队列，等待时间正常发送出去")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "msgId",value = "消息ID",required = true),
            @ApiImplicitParam(name = "message",value = "消息内容",required = true),
            @ApiImplicitParam(name = "timestamp",value = "延迟时间戳",required = true)
    })
    @PostMapping("/delay")
    public R<Void> delay(@RequestParam("msgId") String msgId, @RequestParam("message") String message, @RequestParam("timestamp") Long timestamp) {
        delayQueueService.commit(msgId, message, timestamp);
        return R.ok();
    }

    /**
     * 进入延迟队列，等待时间正常发送出去
     *
     * @param message 消息
     * @param date    时间戳
     * @param pattern 时间格式
     * @return
     */
    @ApiOperation(value = "发送延迟消息",notes = "进入延迟队列，等待时间正常发送出去")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "msgId",value = "消息ID",required = true),
            @ApiImplicitParam(name = "message",value = "消息内容",required = true),
            @ApiImplicitParam(name = "date",value = "延迟时间",required = true),
            @ApiImplicitParam(name = "pattern",value = "时间格式",required = true)
    })
    @PostMapping("/delay/pattern")
    public R<Void> delayPattern(@RequestParam("msgId") String msgId, @RequestParam("message") String message, @RequestParam("date") String date, @RequestParam("pattern") String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date time = null;
        try {
            time = dateFormat.parse(date);
        } catch (ParseException e) {
            log.warn("格式有误！");
        }
        if (time != null) {
            delayQueueService.commit(msgId, message, time.getTime());
        }
        return R.ok();
    }
}
